import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-16
 * Time: 10:44
 */
public class TestDemo {
    //1. 根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(>56)
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            if (n <= 18) {
                System.out.println("少年");
            } else if (n >= 19 && n <= 28) {
                System.out.println("青年");
            } else if (n >= 29 && n <= 55) {
                System.out.println("中年");
            } else {
                System.out.println("老年");
            }
        }
    }

    //2.判定一个数字是否是素数
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            if (n == 2) {
                System.out.println("是素数");
            } else if (n > 2) {
                int i = 0;
                for (i = 2; i <= Math.sqrt(n); i++) {
                    if (n % i == 0) {
                        break;
                    }
                }
                if (i > Math.sqrt(n)) {
                    System.out.println("是素数");
                } else {
                    System.out.println("不是素数");
                }
            } else {
                System.out.println("不是素数");
            }
        }
    }

    //3.打印 1 - 100 之间所有的素数
    public static void main3(String[] args) {
        System.out.println("2");
        for (int n = 3; n <= 100; n += 2) {
            int i = 0;
            for (i = 2; i <= Math.sqrt(n); i++) {
                if (n % i == 0) {
                    break;
                }
            }
            if (i > Math.sqrt(n)) {
                System.out.println(n + " ");
            }
        }
    }

    //4.输出 1000 - 2000 之间所有的闰年
    public static void main4(String[] args) {
        for (int i = 1000; i <= 2000 ; i++) {
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
                System.out.print(i + " ");
            }
        }
    }

    //5.输出乘法口诀表
    public static void main(String[] args) {
        for (int i = 1; i <= 9 ; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d*%d=%-2d ", j, i, i * j);
            }
            System.out.println();
        }
    }

    //6.求两个正整数的最大公约数
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()){
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int m = x % y;
            while (m != 0){
                x = y;
                y = m;
                m = x % y;
            }
            System.out.println(y);
        }
    }

    //7.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
    public static void main7(String[] args) {
        double sum = 0;
        int x = 1;
        for (int i = 1; i <= 100 ; i++) {
            sum += 1.0 / i * x;
            x = -x;
        }
        System.out.println(sum);
    }

    //8. 编写程序数一下 1到 100 的所有整数中出现多少个数字9
    public static void main8(String[] args) {
        int count = 0;
        for (int i = 1; i <= 100 ; i++) {
            if (i % 10 == 9){
                count++;
            } if(i / 10 == 9){
                count++;
            }
        }
        System.out.println(count);
    }

    //9.求出0～999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数，其各位数字的立方和确好等于该数本
    //身，如；153＝1＋5＋3?，则153是一个“水仙花数”。)
    public static void main9(String[] args) {
        for (int i = 0; i <= 999 ; i++) {
            int cnt = 0, ret = 0, sum = 0, n = 0;
            cnt = i;
            while (cnt != 0){
                cnt /= 10;
                n++;
            }
            cnt = i;
           while (cnt != 0){
               ret = cnt % 10;
               cnt /= 10;
               sum += Math.pow(ret, n);
           }
           if (sum == i){
               System.out.println(i);
           }
        }
    }

    //10. 编写代码模拟三次密码输入的场景。 最多能输入三次密码，密码正确，提示“登录成功”,密码错误， 可以重新输
    //入，最多输入三次。三次均错，则提示退出程序
    public static void main10(String[] args) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            String str = scanner.nextLine();
            if (str.equals("abcd")){
                System.out.println("登陆成功");
                break;
            } else{
                System.out.println("密码错误");
            }
        }
    }

    //11.写一个函数返回参数二进制中 1 的个数比如： 15 00001111 4 个 1
    public static void main11(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            int i = 0, sum = 0;
            while (n != 0){
                n = n & (n -1);
                sum++;
            }
            System.out.println(sum);
        }
    }

    //12.获取一个数二进制序列中所有的偶数位和奇数位， 分别输出二进制序列
    public static void main12(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            System.out.println("奇数位");
            for (int i = 30; i >= 0; i -= 2) {
                System.out.print(((n >> i) & 1) + " ");
            }
            System.out.println();
            System.out.println("偶数位");
            for (int j = 31; j >= 1; j -= 2) {
                System.out.print(((n >> j) & 1) + " ");
            }
            System.out.println();
        }
    }

    //13.输出一个整数的每一位.
    public static void main13(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            int ret = 0, cnt = 0, m = 0;
            cnt = n;
            while (cnt > 10){
                m++;
                cnt /= 10;
            }
            while (m >= 0){
                ret = n / (int)Math.pow(10, m);
                System.out.print(ret % 10 + " ");
                m--;
            }
            System.out.println();
        }
    }
}
